package com.ht.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.ht.domain.bo.ArticleBO;
import com.ht.domain.po.Article;
import com.ht.dto.ArticleDTO;
import com.ht.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文章service
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加文章并绑定科普号id
     * @param popularizationId
     * @param articleBO
     * @return
     */
    @Override
    public int addArticle(Long popularizationId, ArticleBO articleBO) {
        try{
            Article article = new Article();
            BeanUtils.copyProperties(articleBO,article);
            String s = RandomUtil.randomStringUpper(5);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            article.setId(s);
            article.setArticleStatus("0");
            article.setPublishedTime(sdf.format(new Date()));
            article.setPopularizationId(popularizationId.toString());
            mongoTemplate.insert(article);
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
